package com.imooc.zhangxiaoxi.lambda.file;

import java.io.*;

/**
 * @Description : 文件服务类
 * @Author : YTY
 * @Date : 2021-01-15 19:18
 */
public class FileService {

    public void fileHandle(String url, FileConsumer fileConsumer) throws IOException {

        // 创建文件读取流
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(url)));

        // 定义行变量和内容sb
        String line;
        StringBuilder stringBuilder = new StringBuilder();

        // 循环读取文件内容
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line + "\n");
        }

        // 调用函数式接口方法，传递文件内容，实现业务逻辑
        fileConsumer.fileHandler(stringBuilder.toString());
    }
}
